package com.kernal.demo.smartvision;

import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kernal.smartvision.activity.SmartvisionCameraActivity;
import com.kernal.smartvision.adapter.VinParseResultAdapter;
import com.kernal.smartvision.ocr.Devcode;
import com.kernal.smartvision.utils.WriteToPCTask;
import com.kernal.smartvision.view.ResultDialog;
import com.kernal.smartvision.view.ResultLayout;
import com.kernal.smartvisionocr.utils.Utills;
import com.kernal.vinparseengine.VinParseInfo;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.HashMap;
import java.util.List;

/**
 * Created by WenTong on 2018/12/26.
 */

public class ShowResultActivity extends AppCompatActivity {
    private RelativeLayout result_top_re;
    LinearLayout result_vin_re;
    private ImageButton result_back, result_ok;
    private TextView result_title, result_tv_feedback;
    private ListView result_vin_list;
    private ImageView result_image;
    private ResultLayout result_view;
    private int srcWidth, srcHeight;
    //VIN 码解析类
    private VinParseInfo vpi;
    private VinParseResultAdapter VPRadapter;
    private List<HashMap<String, String>> vinInfo;


    //表示导入识别还是扫描识别，0 扫描识别，1 导入识别。
    private int startType = 0;
    //表示扫描类型，0 vin, 1 手机号
    private int ocrType = 0;
    //显示的图片
    private String resultPic;
    private String uploadPicPath;
    private String RecogResult = "";


    private View Dialogview;
    private EditText editTextTemp;
    private ImageView image;
    private Bitmap bitmap;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.e("main：：", "识别页面");
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(com.kernal.smartvision.R.layout.activity_show_result);
//        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        Intent resultIntent = getIntent();

        startType = resultIntent.getIntExtra("startType", 0);
        RecogResult = resultIntent.getStringExtra("RecogResult");
        ocrType = resultIntent.getIntExtra("ocrType", 0);

        resultPic = resultIntent.getStringExtra("resultPic");
        uploadPicPath = resultIntent.getStringExtra("uploadPicPath");

        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        srcWidth = displayMetrics.widthPixels;
        srcHeight = displayMetrics.heightPixels;
        com.kernal.smartvision.utils.Utills.initImageLoader(this.getApplicationContext());

    }


    private void initEvent() {
        /**
         * 查询vin 具体信息监听事件
         */
        result_view.setResultChangeListener(new ResultLayout.resultChangeListener() {
            @Override
            public void onChange(String result) {
                //也可以在 finish 中监听。
                if (ocrType == 0) {
                    vpi = new VinParseInfo(ShowResultActivity.this.getApplicationContext());
                    vinInfo = vpi.getVinParseInfo(Devcode.devcode, result);

                    if (vinInfo != null) {
                        VPRadapter = new VinParseResultAdapter(ShowResultActivity.this.getApplicationContext(), vinInfo, srcWidth, srcHeight);
                        result_vin_list.setAdapter(VPRadapter);
                    }
                }
            }

            @Override
            public void onNull() {
            }

            @Override
            public void onFinished(String result) {
            }

            @Override
            public void onBack() {
                backEvent();
            }
        });
        result_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                backEvent();
            }
        });

        result_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ShowResultActivity.this, SmartvisionMainActivity.class));
                finish();
            }
        });

        //报错上传
        result_tv_feedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater inflater = LayoutInflater.from(ShowResultActivity.this.getApplicationContext());
                //root
                Dialogview = inflater.inflate(getResources().getIdentifier("dialog_layout", "layout", getPackageName()), null);
                editTextTemp = (EditText) Dialogview.findViewById(getResources().getIdentifier("dialog_editText_result", "id", getPackageName()));
                image = (ImageView) Dialogview.findViewById(getResources().getIdentifier("dialog_imageView_result", "id", getPackageName()));

                final ResultDialog selfDialog;
                selfDialog = new ResultDialog(ShowResultActivity.this);
                selfDialog.setTitle("报错提示");
                selfDialog.setImage(uploadPicPath);
                selfDialog.setResult(RecogResult);
                selfDialog.setYesOnclickListener("确定", new ResultDialog.onYesOnclickListener() {
                    @Override
                    public void onYesClick() {
                        if (Utills.CheckInternet(ShowResultActivity.this.getApplicationContext())) {
                            String[] httpContent;
                            httpContent = new String[]{resultPic, editTextTemp.getText().toString()};
                            new WriteToPCTask(ShowResultActivity.this).execute(httpContent);
                        } else {
                            Toast.makeText(ShowResultActivity.this, "请检查网络！", Toast.LENGTH_SHORT).show();
                        }
                        selfDialog.dismiss();
                    }
                });
                selfDialog.setNoOnclickListener("取消", new ResultDialog.onNoOnclickListener() {
                    @Override
                    public void onNoClick() {
                        selfDialog.dismiss();
                    }
                });
                selfDialog.show();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Build.VERSION.SDK_INT > 18) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            decorView.setSystemUiVisibility(option);
        }
        initView();
        initEvent();
    }

    // 监听返回键事件
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            backEvent();
            return true;
        }
        return true;
    }

    private void backEvent() {
        startActivity(new Intent(this, SmartvisionMainActivity.class));
        this.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bitmap != null && !bitmap.isRecycled()) {
            bitmap.recycle();
            bitmap = null;
        }
    }

    private void initView() {
        result_top_re = (RelativeLayout) findViewById(com.kernal.smartvision.R.id.result_top_re);
        result_vin_re = (LinearLayout) findViewById(com.kernal.smartvision.R.id.result_vin_re);
        result_back = (ImageButton) findViewById(com.kernal.smartvision.R.id.result_back);
        result_title = (TextView) findViewById(com.kernal.smartvision.R.id.result_title);
        result_tv_feedback = (TextView) findViewById(com.kernal.smartvision.R.id.result_tv_feedback);
        result_ok = (ImageButton) findViewById(com.kernal.smartvision.R.id.result_ok);
        result_vin_list = (ListView) findViewById(com.kernal.smartvision.R.id.result_vin_list);
        result_image = (ImageView) findViewById(com.kernal.smartvision.R.id.result_image);

        if (ocrType == 0) {
            result_view = (ResultLayout) findViewById(com.kernal.smartvision.R.id.result_vin_view);
        } else {
            result_view = (ResultLayout) findViewById(com.kernal.smartvision.R.id.result_phone_view);
        }
        result_view.setVisibility(View.VISIBLE);

        if (RecogResult != null && !RecogResult.equals("")) {
            result_view.setContent(RecogResult);
        }

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                srcWidth, (int) (srcHeight * 0.06));
        layoutParams.topMargin = 0;
        result_top_re.setLayoutParams(layoutParams);

        RelativeLayout.LayoutParams reLayoutParams = new RelativeLayout.LayoutParams(
                (int) (srcWidth * 0.18), (int) (srcHeight * 0.03));
        reLayoutParams.topMargin = (int) (srcHeight * 0.0155);
        reLayoutParams.leftMargin = (int) (srcWidth * 0.02);
        result_back.setLayoutParams(reLayoutParams);

        reLayoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        reLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        result_title.setLayoutParams(reLayoutParams);

        reLayoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        reLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        reLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
        reLayoutParams.rightMargin = (int) (srcWidth * 0.06);
        result_tv_feedback.setLayoutParams(reLayoutParams);

        reLayoutParams = new RelativeLayout.LayoutParams((int) (srcWidth * 0.7), (int) (srcHeight * 0.055));

        reLayoutParams.topMargin = (int) (srcHeight * 0.92);
        reLayoutParams.leftMargin = (int) (srcWidth * 0.15);
        result_ok.setLayoutParams(reLayoutParams);

        if (ocrType == 0) {
            reLayoutParams = new RelativeLayout.LayoutParams((int) (srcWidth * 0.9), (int) (srcHeight * 0.56));
            reLayoutParams.topMargin = (int) (srcHeight * 0.33);
            reLayoutParams.leftMargin = (int) (srcWidth * 0.05);
            result_vin_list.setLayoutParams(reLayoutParams);
            result_vin_list.setVisibility(View.VISIBLE);
            vpi = new VinParseInfo(ShowResultActivity.this);
            vinInfo = vpi.getVinParseInfo(Devcode.devcode, RecogResult);
            if (vinInfo != null) {
                VPRadapter = new VinParseResultAdapter(ShowResultActivity.this, vinInfo, srcWidth, srcHeight);
                result_vin_list.setAdapter(VPRadapter);
            }
        }
        reLayoutParams = new RelativeLayout.LayoutParams((int) (srcWidth * 0.9), (int) (srcHeight * 0.22));
        reLayoutParams.topMargin = (int) (srcHeight * 0.08);
        reLayoutParams.leftMargin = (int) (srcWidth * 0.05);
        result_vin_re.setLayoutParams(reLayoutParams);

        LinearLayout.LayoutParams lLayoutParams = new LinearLayout.LayoutParams((int) (srcWidth * 0.88), (int) (srcHeight * 0.12));
        lLayoutParams.gravity = Gravity.CENTER;
        lLayoutParams.weight = 1;
        result_image.setLayoutParams(lLayoutParams);

        lLayoutParams = new LinearLayout.LayoutParams((int) (srcWidth * 0.88), (int) (srcHeight * 0.10));
        lLayoutParams.gravity = Gravity.CENTER;
        lLayoutParams.weight = 1;
        result_view.setLayoutParams(lLayoutParams);
        if (ocrType == 0) {
            result_title.setText(com.kernal.smartvision.R.string.vin_result);
        } else {
            result_title.setText(com.kernal.smartvision.R.string.phone_result);
        }
        ImageLoader.getInstance().displayImage("file:/" + resultPic, result_image);
    }
}
